/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.chartextensions.configurablelegend;

import JFreeChartExtensions.chartextensions.utils.XYTitleAnnotationWrapper;
import JFreeChartExtensions.ExtendableChartPanel;
import JFreeChartExtensions.JChartMenu.ChartMenuListeners.LegendPropertiesListener;
import JFreeChartExtensions.JChartMenu.JChartMenu;
import JFreeChartExtensions.chartextensions.AbstractStorableChartPanelExtension;
import JFreeChartExtensions.storable.StorableExtenstionObject;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.title.LegendTitle;
import org.jfree.ui.RectangleEdge;

/**
 *
 * @author Nobody
 */
public class ConfigurableLegendExtension extends AbstractStorableChartPanelExtension {

    private JChartMenu setScaleMenuItem;

    public ConfigurableLegendExtension(ExtendableChartPanel panel) {
        super(panel);
        name = "ConfigurableLegendExtension";

    }

    @Override
    public void loadStoredExtension(StorableExtenstionObject extenstionObject) {
        Object[] parameters = extenstionObject.getSerializedObjects();
        Boolean inside = (Boolean) parameters[0];
        Boolean isVisible = (Boolean) parameters[1];
        Double x = (Double) parameters[2];
        Double y = (Double) parameters[3];
        Double w = (Double) parameters[4];
        Double h = (Double) parameters[5];
        String rectName = (String) parameters[6];

        XYTitleAnnotationWrapper annotationWrapper = new XYTitleAnnotationWrapper(chartPanel.getChart());
        annotationWrapper.setPosition(new Rectangle2D.Double(x, y, w, h));
        LegendTitle legendTitle = chartPanel.getChart().getLegend();

        if (!isVisible) {
            annotationWrapper.setVisible(false);
            legendTitle.setVisible(false);
        } else {
            if (inside) {
                annotationWrapper.setVisible(true);
                legendTitle.setVisible(false);
            } else {
                annotationWrapper.setVisible(false);
                legendTitle.setVisible(true);
                if (rectName.equals("RectangleEdge.BOTTOM")) {
                    legendTitle.setPosition(RectangleEdge.BOTTOM);
                } else if (rectName.equals("RectangleEdge.TOP")) {
                    legendTitle.setPosition(RectangleEdge.TOP);
                } else if (rectName.equals("RectangleEdge.LEFT")) {
                    legendTitle.setPosition(RectangleEdge.LEFT);
                } else if (rectName.equals("RectangleEdge.RIGHT")) {
                    legendTitle.setPosition(RectangleEdge.RIGHT);
                }
            }
        }
    }

    @Override
    public StorableExtenstionObject storeExtension() {
        XYTitleAnnotationWrapper annotationWrapper = new XYTitleAnnotationWrapper(chartPanel.getChart());
        LegendTitle legendTitle = chartPanel.getChart().getLegend();
        RectangleEdge rectangleEdge = legendTitle.getPosition();
        boolean insideChart = annotationWrapper.isVisible();
        boolean isVisible = legendTitle.isVisible() || annotationWrapper.isVisible();
        double x = 0, y = 0, w = 0, h = 0;
        if (insideChart) {
            x = annotationWrapper.getAnnotation().getX();
            y = annotationWrapper.getAnnotation().getY();
            w = annotationWrapper.getAnnotation().getMaxWidth();
            h = annotationWrapper.getAnnotation().getMaxWidth();
        }
        Object[] objectMap = new Object[7];
        objectMap[0] = insideChart;
        objectMap[1] = isVisible;
        objectMap[2] = x;
        objectMap[3] = y;
        objectMap[4] = w;
        objectMap[5] = h;
        objectMap[6] = rectangleEdge.toString();
        return new StorableExtenstionObject(objectMap, name);
    }

    @Override
    public void applyExtension(ChartPanel chartPanel) {
        this.chartPanel = (ExtendableChartPanel) chartPanel;
        setScaleMenuItem = new JChartMenu("Legend properties...");
        setScaleMenuItem.addListener(new LegendPropertiesListener((ExtendableChartPanel) chartPanel));
        chartPanel.getPopupMenu().add(setScaleMenuItem);
    }

    @Override
    public void removeExtension() {
        chartPanel.getPopupMenu().remove(setScaleMenuItem);
    }
}
